﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GM.Model.SubSonic;

namespace GM.Forms
{
    public partial class AssetHistoryForm : Form
    {
        private readonly List<gm_srodki_trwale_ruchy> m_ruchy;
        private readonly List<Pracownik> m_pracownicy;

        public AssetHistoryForm(IEnumerable<gm_srodki_trwale_ruchy> ruchy, IEnumerable<Pracownik> pracownicy)
        {
            m_ruchy = new List<gm_srodki_trwale_ruchy>(ruchy);
            m_pracownicy = new List<Pracownik>(pracownicy);

            InitializeComponent();
        }

        public void ShowHistory(gm_srodki_trwale ruchy)
        {
            if (ruchy == null)
                return;
            olvHistory.ClearObjects();
            var historia = m_ruchy.Where(x => x.srodek_id == ruchy.nr_ewidencyjny).ToList();
            foreach (var srodkiTrwaleRuchy in historia)
            {
                var osoba = m_pracownicy.First(x => x.id_prac == srodkiTrwaleRuchy.pracownik_id);
                srodkiTrwaleRuchy.Osoba = osoba != null
                                              ? string.Format("{0} {1}", osoba.imie, osoba.nazwisko)
                                              : "brak danych";
            }
            olvHistory.SetObjects(historia);
            Show();
            BringToFront();
        }

        private void AssetHistoryForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            Hide();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Hide();
        }
    }
}
